const c = window.cloud
Vue.prototype.cloud = c
    // 将js-cookie库 挂载到Vue的原型对象上
Vue.prototype.Cookie = window.Cookies
Vue.config.productionTip = false //阻止vue在启动时生成生产提示
Vue.config.devtools = true;

/* 
1.login设置为主index界面，一进入对cookie进行判断，且进行云数据库中的判定
如果为空则正常进行登录，
如果不为空则直接进入人员管理界面
2.登录成功写入cookie，
3.每个界面判断有没有cookie，没有则跳转login
*/
var vm = new Vue({
    el: "#loginfor",
    data() {
        return {
            userEmail: "",
            userPassword: "",

        }
    },
    methods: {

        resetInput() {
            this.userEmail = '';
            this.userPassword = ''
        },
        async initWx() {
            //创建新的cloud实例
            var c1 = new this.cloud.Cloud({
                    identityless: true,
                    // 资源方 AppID 填自己的
                    resourceAppid: 'wx96f760966921f98a',
                    // 资源方环境 ID	填自己的
                    resourceEnv: 'cloud1-8g54d6xb74e96e7d'
                })
                //初始化,使用await将异步转化为同步
            await c1.init()
            console.log('初始化完成');
            this.cloud = c1
            this.db = this.cloud.database()
        },

        register() {
            const loading = this.$loading({
                lock: true,
                text: '校验中',
                background: 'rgba(255, 255, 255, 0.7)'
            });
            console.log(this.usersid, this.userspass)
            console.log(1)
            var usersid = this.userEmail
            var userPassword = this.userPassword
            if (userPassword != '' && usersid != '') {
                this.db.collection('operationUsers').where({ email: usersid }).get({
                    success: res => {
                        console.log(res);
                        console.log("密码为" + res.data[0].webPassword);
                        if (res.data[0].webPassword == userPassword) {
                            console.log("密码与账户密码一致")
                                // alert("密码正确！但登录功能暂未完善，敬请期待")
                            this.writeCookie(res.data[0])

                            location.href = './usersmanage.html'
                        } else {
                            console.log("密码错误")
                            alert("密码错误！")
                        }
                    },
                    fail: res => {
                        console.log(res);
                    },
                    complete: () => {
                        loading.close();
                    }
                })
            } else {

                alert("请输入完整账号信息")
            }




            /////////////////////////////////////////////////////////
        },
        // 验证cookie
        checkCookie() {
            // 如果存在cookie
            if (this.Cookie.get("personInfo")) {
                let info = JSON.parse(this.Cookie.get("personInfo"));
                if (info) {
                    console.log("用户已经登录");
                    console.log("本地cookie为");
                    // console.log(info);
                    //对cookie内容进行校验
                    console.log(info._openid);
                    this.db.collection("operationUsers").where({
                        _openid: info._openid
                    }).get().then(res => {
                        console.log("查询的cookie信息", res);
                        if (res.data[0].webPassword == info.webPassword) {
                            console.log("cookie正确");
                            location.href = "./membermanagement.html"
                        } else {
                            alert("请勿盗用他人cookie")
                        }
                    })
                } else {
                    console.log("用户未登录");
                    // let personInfo={a:1,b:2}
                    // let jsontoString=JSON.stringify(personInfo)
                    // this.Cookie.set("personInfo",jsontoString,{expires:1})
                }
            }

        },
        writeCookie(personInfo) {
            this.Cookie.set("personInfo", JSON.stringify(personInfo), { expires: 7 })
            console.log(this.Cookie.get("personInfo"));
        }

    },
    async created() {
        await this.initWx()
        this.checkCookie();
    },
})